package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.FailLog;
import cn.wolfcode.crm.domain.Transfer;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.service.IFailLogService;
import cn.wolfcode.crm.service.ITransferService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("client")
public class ClientController {

    @Autowired
    private IClientService clientService;
    @Autowired
    private ITransferService transferService;
    @Autowired
    private IFailLogService failLogService;

    @RequestMapping("potential")
    @RequiresPermissions(value = {"client:view","客户页面"},logical = Logical.OR)
    public String view(){
        return "client/potentialClient";
    }
    @RequestMapping("formal")
    @RequiresPermissions(value = {"client:view","客户页面"},logical = Logical.OR)
    public String view2(){
        return "client/formalClient";
    }
    @RequestMapping("source")
    @RequiresPermissions(value = {"client:view","池页面"},logical = Logical.OR)
    public String view3(){
        return "client/sourceClient";
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Client> selectAll(){
        return clientService.selectAll();
    }

  

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"client:saveOrUpdate","潜在客户新增/修改"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Client entity) {
        JsonResult jsonResult = new JsonResult();
        try{
            clientService.saveOrUpdate(entity);
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }
    //更改负责人
    @RequestMapping("changePrincipal")
    @ResponseBody
    public JsonResult changePrincipal(Transfer entity,Long clientId) {
        JsonResult jsonResult = new JsonResult();
        try{
            transferService.insert(entity);
            if (entity.getNewSeller() == null){
                clientService.changePrincipal(null,clientId);
                clientService.changeState(13L,clientId);
            }else {
                clientService.changePrincipal(entity.getNewSeller().getId(), entity.getClient().getId());
            }
        }catch (Exception e){
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }
    //设置开发失败/流失
    @RequestMapping("saveFail")
    @ResponseBody
    public JsonResult saveFail(FailLog entity) {
        JsonResult jsonResult = new JsonResult();
        try{
            failLogService.insert(entity);
            clientService.changePrincipal(entity.getPrincipal().getId(),entity.getClient().getId());
            if (entity.getState() == 0){
                clientService.changeState(14L,entity.getClient().getId());
            }else{
                clientService.changeState(15L,entity.getClient().getId());
            }
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }
    //吸纳
    @RequestMapping("absorbClient")
    @ResponseBody
    public JsonResult absorbClient(Long id) {
        JsonResult jsonResult = new JsonResult();
        try{
            clientService.absorbClient(id);
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions(value = {"client:delete","潜在客户删除"},logical = Logical.OR)
    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try{
            clientService.deleteByPrimaryKey(id);
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }

    @RequestMapping("changeFormal")
    @ResponseBody
    public JsonResult formal(Long id) {
        JsonResult jsonResult = new JsonResult();
        try{
            clientService.changeState(-1L,id);
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }


    

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(ClientQueryObject qo) throws Exception{
        return clientService.query(qo);
    }

}
